<template>
	<view style="background-color: #F2F6FC;height: 100vh;">
		<view style="background-color: #fff;">
			<u-skeleton rows="6" :loading="detailLoading">
				<!-- 商品图 -->
				<view>
					<u-image :fade="true" duration="450" :src="storeData.image" width="100%" height="225px"></u-image>
				</view>
				<!-- 描述 -->
				<view style="padding: 15px;">
					<text class="grid-text_goods">{{storeData.homeName}}</text>
					<view style="display: flex; padding: 10px 0 10px 0px;">
						<text class="text_common">地址：<text style="color: #82848a;">{{storeData.address}}</text></text>
					</view>
					<view style="display: flex; justify-content: space-between; align-items: center;">
						<text class="text_common">简介：<text class="text_common">{{storeData.remark}}</text></text>
					</view>
					<view style="display: flex;justify-content: flex-end;">
						<view style="width: 50px;">
							<u-button @click="handleCollect" :plain="true" iconColor="#71d5a1" text="订阅"></u-button>
						</view>
					</view>
				</view>
			</u-skeleton>
		</view>

		<!-- 详情和评论 -->
		<view style="margin-top: 10px; width: 100%; height: auto;background-color: #fff;">
			<u-tabs :scrollable=false :list="list1" @click="changeOpen"></u-tabs>

			<view style="padding: 0 10px 0 10px">
				<!-- 商品数据 -->
				<view v-if="open">
					<u-list width="100%" @scrolltolower="scrolltolower">
						<u-list-item v-for="(item, index) in goodsList" :key="index">
							<view class="card" @click="toDetails(item.id)">
								<view>
									<image style="width: 80px; height: 80px;border-radius: 8px;" :src="item.image">
									</image>
								</view>
								<view style="margin-left: 10px;padding-top: 5px">
									<view style="margin-bottom: 6px;">
										<text class="goods_name">{{item.name}}</text>
									</view>
									<view style="display: flex;">
										<u-icon class="text" size="20" name="rmb" color="#DC143C"></u-icon><text
											style="color: #DC143C; font-size: 17px;font-weight: bold;">{{item.price}}</text>
									</view>
								</view>
							</view>
						</u-list-item>
					</u-list>
				</view>

				<!-- 店铺评论 -->
				<view v-else>
					<!-- 空评论 -->
					<view v-if="isEmpty">
						<u-empty mode="message" icon="http://cdn.uviewui.com/uview/empty/car.png">
						</u-empty>
					</view>

					<view v-else class="comment" v-for="(item, index) in commentList" :key="index">
						<view class="left">
							<image :src="item.avatar" mode="aspectFill"></image>
						</view>
						<view class="right">
							<view class="top">
								<view class="name">{{ item.username }}</view>
								<view>
									<u-rate :readonly=true v-model="item.score" :allowHalf=true></u-rate>
								</view>
							</view>
							<view class="content">{{ item.comment }}</view>
							<view class="bottom">
								{{ item.createTime }}
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		queryStoreDetail,
		queryCommentList,
		queryGoodsList
	} from '@/api/store/index.js';
	import {
		doCollectStore
	} from '@/api/collectstore/index.js';
	import {
		ref
	} from "vue";
	import {
		getToken,
		getUserInfo
	} from "@/stores/index.js";
	
	const token = getToken();
	const user = getUserInfo();
	const storeId = ref(null);
	const detailLoading = ref(true);
	onLoad((option) => {
		storeId.value = option.storeId;
		getStoreData();
		getGoodsList();
		getCommentList();
		detailLoading.value = false;
	})

	//获取店铺信息
	const storeData = ref({});
	const getStoreData = () => {
		queryStoreDetail(storeId.value).then((res) => {
			if (res.code === 200) {
				storeData.value = res.data;
			}
		})
	}

	const goodsList = ref({});
	//获取商品数据
	const getGoodsList = () => {
		queryGoodsList(storeId.value).then((res) => {
			if (res.code === 200) {
				goodsList.value = res.data;
			}
		})
	}

	const list1 = ref([{
		name: '宝贝',
	}, {
		name: '评论',
	}])
	const open = ref(true);
	const changeOpen = () => {
		open.value = !open.value;
	}

	//获取店铺评论
	const commentList = ref([]);
	const isEmpty = ref(true);
	const getCommentList = () => {
		queryCommentList(storeId.value).then((res) => {
			if (res.code === 200) {
				commentList.value = res.data;
				if (commentList.value.length > 0) {
					isEmpty.value = false;
				}
			}
		})
	}

	//底部触发事件
	const scrolltolower = () => {
		console.log("哎哟喂")
	}

	//跳往商品详情页
	const toDetails = (goodsId) => {
		uni.navigateTo({
			url: "/pages/detail/detail?goodsId=" + goodsId
		})
	}
	
	//收藏
	const collectStore = ref({
		userId: null,
		storeId: null
	})
	const handleCollect = () => {
		collectStore.value.storeId = storeId.value;
		if (token === '') {
			uni.redirectTo({
				url: "/pages/login/login"
			})
		}else{
			collectStore.value.userId = user.id;
			doCollectStore(collectStore.value).then((res) => {
				if (res.code === 200) {
					uni.showToast({
						title: "订阅成功!"
					})
				}
			})
		}
	}
</script>

<style scoped lang="scss">
	.card {
		padding: 5px;
		display: flex;
		margin-top: 15px;
		height: 90px;
		background-color: #F5F7FA;
		border-radius: 6px;
	}

	.card:active {
		background-color: #fff;
		opacity: 1;
	}

	.grid-text_goods {
		font-size: 17px;
		color: #000000;
		font-weight: bold;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
		text-overflow: ellipsis;
		font-family: "华文楷体";
	}


	.goods_name {
		font-size: 14px;
		color: #000000;
		font-weight: bold;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
		text-overflow: ellipsis;
		font-family: "华文楷体";
	}

	.text_common {
		color: #909399;
		font-size: 12px;
		margin-right: 15px;
	}

	.a_style:hover {
		color: coral;
	}

	.text {
		font-size: 13px;
		color: #909399;
		/* #ifndef APP-PLUS */
		box-sizing: border-box;
		padding-top: 8px;
		margin-bottom: 20px;
		/* #endif */
	}

	.comment {
		display: flex;
		padding: 30rpx;

		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}

		.right {
			flex: 1;
			padding-left: 20rpx;
			font-size: 30rpx;

			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;

				.name {
					color: #5677fc;
				}
			}

			.content {
				margin-bottom: 10rpx;
			}

			.bottom {
				margin-top: 20rpx;
				display: flex;
				font-size: 24rpx;
				color: #9a9a9a;
			}
		}
	}
</style>